define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () 
        {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/order/index',
                    del_url: 'product/order/del',
                    info_url:'product/order/info',
                    deliver_url:'product/order/deliver',
                    refund_url:'product/order/refund',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable:true, operate: false},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'amount', title: __('Amount'), sortable:true, operate:'BETWEEN'},
                        {field: 'business.nickname', title: __('Busid'), operate: 'LIKE'},
                        {field: 'express.name', title: __('Expressid'), operate: 'LIKE'},
                        {field: 'expresscode', title: __('Expresscode'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'),searchList: Config.statuslist, operate: 'LIKE',formatter: Table.api.formatter.flag},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: '订单详情',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-success btn-dialog",
                                    icon: 'fa fa-eye',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                },
                                {
                                    name:'deliver',
                                    title:'发货',
                                    extend: 'data-toggle="tooltip"',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url:$.fn.bootstrapTable.defaults.extend.deliver_url,
                                    icon:'fa fa-leaf',
                                    visible: (row) => (row.status == 1 || row.status == 2),
                                },
                                {
                                    name:'refund',
                                    title: '退货审核',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-success btn-dialog",
                                    icon:'fa fa-truck',
                                    url:$.fn.bootstrapTable.defaults.extend.refund_url,
                                    visible: (row) => (row.status == '-1' || row.status == '-2')
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        info: function () 
        {
            Controller.api.bindevent();
        },
        del:function()
        {
            Controller.api.bindevent();
        },
        deliver:function()
        {
            Controller.api.bindevent();
        },
        refund:function()
        {
            $('#refund').change(function(){
                let val = $(this).val()

                if(val == 1)
                {
                    $('#examinereason').hide()
                    $('#examinereason textarea').val('')
                }else
                {
                    $('#examinereason').show()
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});
